/*-----------------------------------------------------------------------------

 Copyright 2017 Hopsan Group

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.


 The full license is available in the file LICENSE.
 For details about the 'Hopsan Group' or information about Authors and
 Contributors see the HOPSANGROUP and AUTHORS files that are located in
 the Hopsan source code root directory.

-----------------------------------------------------------------------------*/

#ifndef AEROJETENGINE_HPP_INCLUDED
#define AEROJETENGINE_HPP_INCLUDED

#include <iostream>
#include "ComponentEssentials.h"
#include "ComponentUtilities.h"
#include "math.h"

//!
//! @file AeroJetEngine.hpp
//! @author Petter Krus <petter.krus@liu.se>
//! @date Thu 24 Jul 2014 13:01:04
//! @brief Calulates the mass of remaining fuel in tank
//! @ingroup AeroComponents
//!
//==This code has been autogenerated using Compgen==
//from 
/*{, C:, HopsanTrunk, componentLibraries, defaultLibrary, Special, \
AeroComponents}/AeroJetEngineF110.nb*/

using namespace hopsan;

class AeroJetEngine : public ComponentQ
{
private:
     double thrustmax;
     double SFC0;
     double thau;
     double Ctm;
     double Csfct;
     double Csfcm;
     double e;
     double delayParts1[9];
     double delayParts2[9];
     Matrix jacobianMatrix;
     Vec systemEquations;
     Matrix delayedPart;
     int i;
     int iter;
     int mNoiter;
     double jsyseqnweight[4];
     int order[1];
     int mNstep;
//==This code has been autogenerated using Compgen==
     //inputVariables
     double uin;
     double rho;
     double T;
     double p0;
     double Vsound;
     double speed;
     //outputVariables
     double thrust;
     double Shspeed;
     double qmfuel;
     //LocalExpressions variables
     double mach;
     double Shspeedref;
     double SFC;
     //Expressions variables
     //Delay declarations
//==This code has been autogenerated using Compgen==
     //inputVariables pointers
     double *mpuin;
     double *mprho;
     double *mpT;
     double *mpp0;
     double *mpVsound;
     double *mpspeed;
     //inputParameters pointers
     double *mpthrustmax;
     double *mpSFC0;
     double *mpthau;
     double *mpCtm;
     double *mpCsfct;
     double *mpCsfcm;
     double *mpe;
     //outputVariables pointers
     double *mpthrust;
     double *mpShspeed;
     double *mpqmfuel;
     Delay mDelayedPart10;
     Delay mDelayedPart11;
     EquationSystemSolver *mpSolver;

public:
     static Component *Creator()
     {
        return new AeroJetEngine();
     }

     void configure()
     {
//==This code has been autogenerated using Compgen==

        mNstep=9;
        jacobianMatrix.create(1,1);
        systemEquations.create(1);
        delayedPart.create(2,6);
        mNoiter=2;
        jsyseqnweight[0]=1;
        jsyseqnweight[1]=0.67;
        jsyseqnweight[2]=0.5;
        jsyseqnweight[3]=0.5;


        //Add ports to the component
        //Add inputVariables to the component
            addInputVariable("uin","Throttle setting 0-1","",1.,&mpuin);
            addInputVariable("rho","The density at altitude \
h","kg/m3",1.25,&mprho);
            addInputVariable("T","Temperature at altitude h","K",273.,&mpT);
            addInputVariable("p0","Pressure at altitude \
h","Pa",100000.,&mpp0);
            addInputVariable("Vsound","Speed of sound at altitude \
h","m/s",340.,&mpVsound);
            addInputVariable("speed","Air speed","m/s",100.,&mpspeed);

        //Add inputParammeters to the component
            addInputVariable("thrustmax", "Max thrust at Mach 0 and sea \
level", "N", 77000.,&mpthrustmax);
            addInputVariable("SFC0", "Nominal thrust specific fuel, at \
M=1,2", "kg/(N s)", 0.0000266,&mpSFC0);
            addInputVariable("thau", "Engine time constant", "s", \
5.,&mpthau);
            addInputVariable("Ctm", "mach-thrust coefficient", "", \
0.1304,&mpCtm);
            addInputVariable("Csfct", "thrust-SFC coefficient", "", \
0.1304,&mpCsfct);
            addInputVariable("Csfcm", "mach-SFC coefficient", "", \
0.13045134663401256,&mpCsfcm);
            addInputVariable("e", "e", "", 2.71828,&mpe);
        //Add outputVariables to the component
            addOutputVariable("thrust","Thrust","N",5000.,&mpthrust);
            addOutputVariable("Shspeed","Normalized engine shaft \
speed","rad/s",1.,&mpShspeed);
            addOutputVariable("qmfuel","Fuel, mass \
flow","kg/s",1.,&mpqmfuel);

//==This code has been autogenerated using Compgen==
        //Add constantParameters
        mpSolver = new EquationSystemSolver(this,1);
     }

    void initialize()
     {
        //Read port variable pointers from nodes

        //Read variables from nodes

        //Read inputVariables from nodes
        uin = (*mpuin);
        rho = (*mprho);
        T = (*mpT);
        p0 = (*mpp0);
        Vsound = (*mpVsound);
        speed = (*mpspeed);

        //Read inputParameters from nodes
        thrustmax = (*mpthrustmax);
        SFC0 = (*mpSFC0);
        thau = (*mpthau);
        Ctm = (*mpCtm);
        Csfct = (*mpCsfct);
        Csfcm = (*mpCsfcm);
        e = (*mpe);

        //Read outputVariables from nodes
        thrust = (*mpthrust);
        Shspeed = (*mpShspeed);
        qmfuel = (*mpqmfuel);

//==This code has been autogenerated using Compgen==

        //LocalExpressions
        mach = speed/Vsound;
        thrust = \
0.8207*Power(e,0.1304*mach)*Power(rho,0.8853)*Power(Shspeed,3.104)*thrustmax;
        Shspeedref = Power(limit(uin,0,1),0.323);
        SFC = \
(0.000017638*Power(e,0.4453*mach)*Power(Shspeed,0.00459))/Power(rho,0.126478)\
;

        //Initialize delays
        delayParts1[1] = (mTimestep*Shspeed - mTimestep*Shspeedref - \
2*Shspeed*thau)/(mTimestep + 2*thau);
        mDelayedPart11.initialize(mNstep,delayParts1[1]);

        delayedPart[1][1] = delayParts1[1];
     }
    void simulateOneTimestep()
     {
        Vec stateVar(1);
        Vec stateVark(1);
        Vec deltaStateVar(1);

        //Read variables from nodes

        //Read inputVariables from nodes
        uin = (*mpuin);
        rho = (*mprho);
        T = (*mpT);
        p0 = (*mpp0);
        Vsound = (*mpVsound);
        speed = (*mpspeed);

        //LocalExpressions
        mach = speed/Vsound;
        thrust = \
0.8207*Power(e,0.1304*mach)*Power(rho,0.8853)*Power(Shspeed,3.104)*thrustmax;
        Shspeedref = Power(limit(uin,0,1),0.323);
        SFC = \
(0.000017638*Power(e,0.4453*mach)*Power(Shspeed,0.00459))/Power(rho,0.126478)\
;

        //Initializing variable vector for Newton-Raphson
        stateVark[0] = Shspeed;

        //Iterative solution using Newton-Rapshson
        for(iter=1;iter<=mNoiter;iter++)
        {
         //JetEngine
         //Differential-algebraic system of equation parts

          //Assemble differential-algebraic equations
          systemEquations[0] =Shspeed - (mTimestep*Shspeedref)/(mTimestep + \
2*thau) + delayedPart[1][1];

          //Jacobian matrix
          jacobianMatrix[0][0] = 1;
//==This code has been autogenerated using Compgen==

          //Solving equation using LU-faktorisation
          mpSolver->solve(jacobianMatrix, systemEquations, stateVark, iter);
          Shspeed=stateVark[0];
          //Expressions
          qmfuel = SFC*thrust;
        }

        //Calculate the delayed parts
        delayParts1[1] = (mTimestep*Shspeed - mTimestep*Shspeedref - \
2*Shspeed*thau)/(mTimestep + 2*thau);

        delayedPart[1][1] = delayParts1[1];

        //Write new values to nodes
        //outputVariables
        (*mpthrust)=thrust;
        (*mpShspeed)=Shspeed;
        (*mpqmfuel)=qmfuel;

        //Update the delayed variabels
        mDelayedPart11.update(delayParts1[1]);

     }
    void deconfigure()
    {
        delete mpSolver;
    }
};
#endif // AEROJETENGINE_HPP_INCLUDED
